﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using D3DSS.DataContract;
using D3DSS.Http;
using System.Configuration;
using D3DSS.FormUI.Properties;

namespace D3DSS.FormUI
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            if (Settings.Default.X <= 0 || Settings.Default.Y <= 0)
            {
                this.StartPosition = FormStartPosition.CenterScreen;
                Settings.Default.X = this.Location.X;
                Settings.Default.Y = this.Location.Y;
                Settings.Default.Save();
            }
            else
            {
                this.Location = new Point(Settings.Default.X, Settings.Default.Y);
                this.StartPosition = FormStartPosition.Manual;
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            Program.MainHandle = Handle;
            Program.Result = DialogResult.OK;
            KeyHook.RegKey();

            gbDropStatistics.Enabled = false;

            Program.FormStatisticsOrder = new frmStatisticsOrder();
            Program.FormQueryStatistics = new frmQueryStatistics();
            Program.FormSetup = new frmSetup();

            this.Icon = Resources.Icon;
            niMain.Icon = Resources.Icon;
            Program.FormStatisticsOrder.Icon = Resources.Icon;
            Program.FormQueryStatistics.Icon = Resources.Icon;
            Program.FormSetup.Icon = Resources.Icon;

            Program.FormStatisticsOrder.FormClosed += new FormClosedEventHandler(statisticsOrder_FormClosing);
        }

        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            if (m.Msg == 0x0312)
            {
                this.WindowState = FormWindowState.Normal;
                this.Show();
                this.Activate();
            }
            base.WndProc(ref m);
        }

        private void btnCreateStatisticsOrder_Click(object sender, EventArgs e)
        {
            Program.FormStatisticsOrder.ShowDialog();
            Program.FormStatisticsOrder.Activate();
        }

        private void statisticsOrder_FormClosing(object sender, EventArgs e)
        {
            if (Program.StatisticsOrder != null)
            {
                lblGameServerText.Text = Program.StatisticsOrder.GameServer;
                lblGameRoleText.Text = Program.StatisticsOrder.GameRole;
                lblActText.Text = Program.StatisticsOrder.Act;
                lblInitMFText.Text = Program.StatisticsOrder.MF;
                lblRoomIPText.Text = Program.StatisticsOrder.RoomIP;

                gbDropStatistics.Enabled = true;
                nudCurrentMF.Value = int.Parse(Program.StatisticsOrder.MF);
                nudNephalem.Value = nudLegendDrop.Value = nudLevel61RaraDrop.Value = nudLevel62RaraDrop.Value = nudLevel63RaraDrop.Value = nudMagicDrop.Value = nudRaraDrop.Value = nudTotalDrop.Value = nudWhiteDrop.Value = 0;
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            

            var request = new DropRequest();

            if (chkIsExtend.Checked)
            {
                if (nudTotalDrop.Value == 0)
                {
                    MessageBox.Show("总掉落数不能为0！");
                    return;
                }
                else
                {
                    if (nudWhiteDrop.Value + nudMagicDrop.Value + nudRaraDrop.Value + nudLegendDrop.Value != nudTotalDrop.Value)
                    {
                        MessageBox.Show("总掉落数和其余数量总和不相等！");
                        return;
                    }
                }
            }

            this.WindowState = FormWindowState.Minimized;

            request.IsExtend = chkIsExtend.Checked;
            request.LegendDrop = (int)nudLegendDrop.Value;
            request.Level61RaraDrop = (int)nudLevel61RaraDrop.Value;
            request.Level62RaraDrop = (int)nudLevel62RaraDrop.Value;
            request.Level63RaraDrop = (int)nudLevel63RaraDrop.Value;
            request.MagicDrop = (int)nudMagicDrop.Value;
            request.MF = (int)nudCurrentMF.Value;
            request.Nephalem = (int)nudNephalem.Value;
            request.RaraDrop = (int)nudRaraDrop.Value;
            request.RoomIP = int.Parse(lblRoomIPText.Text.Split('.')[1]);
            request.RoomParentIP = int.Parse(lblRoomIPText.Text.Split('.')[0]);
            request.StatisticsOrderID = Program.StatisticsOrder.StatisticsOrderID;
            request.TotalDrop = (int)nudTotalDrop.Value;
            request.WhiteDrop = (int)nudWhiteDrop.Value;

            var response = HttpRequest.Post<Response<DropResponse>>(Settings.Default.CreateDropUrl, request, Program.Token);
            if (response.Item1.IsSuccess)
            {
                if (nudNephalem.Value < 5)
                {
                    nudCurrentMF.Value += 15;
                    nudNephalem.Value++;
                }
                nudLegendDrop.Value = nudLevel61RaraDrop.Value = nudLevel62RaraDrop.Value = nudLevel63RaraDrop.Value = nudMagicDrop.Value = nudRaraDrop.Value = nudTotalDrop.Value = nudWhiteDrop.Value = 0;
            }
            else
            {
                MessageBox.Show(response.Item1.MessageDictionary.Single(m => m.MessageCode == response.Item1.Code).MessageContent);
            }
        }

        private void tmiDisplay_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.Show();
            this.Activate();
        }

        private void tmiSetup_Click(object sender, EventArgs e)
        {
            Program.FormSetup.ShowDialog();
            Program.FormSetup.Activate();
        }

        private void tmiQueryStatistics_Click(object sender, EventArgs e)
        {
            Program.FormQueryStatistics.ShowDialog();
            Program.FormQueryStatistics.Activate();
        }

        private void tmiExit_Click(object sender, EventArgs e)
        {
            Program.Result = DialogResult.No;
            this.Close();
        }

        private void chkIsExtend_CheckedChanged(object sender, EventArgs e)
        {
            nudTotalDrop.Enabled = nudWhiteDrop.Enabled = nudMagicDrop.Enabled = nudLegendDrop.Enabled = chkIsExtend.Checked;
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Settings.Default.X = this.Location.X;
            Settings.Default.Y = this.Location.Y;
            Settings.Default.Save();

            if (Program.Result == DialogResult.OK)
            {
                this.WindowState = FormWindowState.Minimized;
                e.Cancel = true;
            }
            else
            {
                KeyHook.UnRegKey(Program.MainHandle, Settings.Default.HotKeyID);
            }
        }
    }
}
